/*
 * $Id: XmlParserFactory.java,v 1.3 2009/06/16 19:40:41 afaichun Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.onwatch.xml.parser;

import javax.xml.parsers.ParserConfigurationException;

import org.springframework.util.Assert;
import org.xml.sax.SAXException;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.database.dao.IXmlParserInfoDAO;
import com.ge.healthcare.autosc.common.database.pojo.CfgXmlParserInfo;
import com.ge.healthcare.autosc.onwatch.exception.XmlParserFactoryException;

public class XmlParserFactory {

	private static IXmlParserInfoDAO xmlParserInfoDAO; 
	
	public void init() {
		Assert.notNull(xmlParserInfoDAO, 
		"xmlParserInfoDAO is not defined. Use setXmlParserInfoDAO to provide one");
	}
	
	public static IXmlParser getXmlParser(String parserName) 
		throws XmlParserFactoryException {
		String methodName = "getXmlParser";
		IXmlParser parser = null;
		if(parserName != null) {
			ASCLogger.debug(XmlParserFactory.class, methodName, 
						"get parser with parser name " + parserName);
			CfgXmlParserInfo xmlParserInfo = xmlParserInfoDAO.getXmlParserInfoByName(parserName);
			if(xmlParserInfo != null) {
				try {
					parser = new StatusReportParser(xmlParserInfo.getXmlSchemaFile());
				} catch (ParserConfigurationException pce) {
					String errorStr = "Failed creating Xml Parser for class " + xmlParserInfo.getXmlParserClass();
					throw new XmlParserFactoryException(errorStr, pce);
				} catch (SAXException se) {
					String errorStr = "Failed creating Xml Parser for class " + xmlParserInfo.getXmlParserClass();
					throw new XmlParserFactoryException(errorStr, se);
				}
			} else {
				ASCLogger.error(XmlParserFactory.class, methodName, 
						"CfgParserInfo not found for parser name " + parserName);
			}
		}
		return parser;
	}
	
	public void setXmlParserInfoDAO(IXmlParserInfoDAO xmlParserInfoDAO) {
		XmlParserFactory.xmlParserInfoDAO = xmlParserInfoDAO;
	}
}
